#include <ClanLib/core.h>
#include <ClanLib/application.h>
#include <ClanLib/display.h>
#ifdef USE_SDL
	#include <ClanLib/sdl.h>
#else
	#include <ClanLib/gl.h>
#endif
#include "blockGuy.h"

BlockGuy::BlockGuy()
{
}

BlockGuy::BlockGuy(CL_Sprite &sprite, bool &jump_key, bool &left_key, bool &right_key)
: Entity(sprite)
{
	vel = CL_Vector2(0.0f, 0.0f);
	accel = CL_Vector2(0.0f, 350.0f);

	gravity = 1000.0f;
	jumpBoost = 350.0f;

	maxSpeed = 100.0f;
	groundCoefficient = 5.0f;
	airCoefficient = 3.0f;
	frictionCoefficient = 10.0f;
	
	key_jump = &jump_key;
	key_left = &left_key;
	key_right = &right_key;
}

BlockGuy::BlockGuy(CL_Sprite &sprite, CL_Pointf &initial_position, bool &jump_key, bool &left_key, bool &right_key)
: Entity(sprite, initial_position)
{
	vel = CL_Vector2(0.0f, 0.0f);
	accel = CL_Vector2(0.0f, 350.0f);

	gravity = 1000.0f;
	jumpBoost = 350.0f;

	maxSpeed = 100.0f;
	groundCoefficient = 5.0f;
	airCoefficient = 3.0f;
	frictionCoefficient = 10.0f;

	key_jump = &jump_key;
	key_left = &left_key;
	key_right = &right_key;
}

BlockGuy::~BlockGuy()
{
}

inline void BlockGuy::logic(float &time)
{
	if(onGround){
		if(*key_jump)
			vel.y = -300.0f;
		else if(*key_right)
			accel.x = groundCoefficient*(maxSpeed-vel.x);
		else if(*key_left)
			accel.x = -1*groundCoefficient*(maxSpeed+vel.x);
		else
			accel.x = -1*frictionCoefficient*vel.x;
	} else {
		if(*key_jump)
			accel.y = gravity - jumpBoost;
		else
			accel.y = gravity;
		if(*key_right)
			accel.x = airCoefficient*(maxSpeed-vel.x);
		else if (*key_left)
			accel.x = -1*airCoefficient*(maxSpeed+vel.x);
		else
			accel.x = -1*frictionCoefficient*vel.x;
	}
}
